﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vensoft.Entities;
using vensoft.BussinesLogic;

namespace vensoft.Win32
{
    public partial class frmAEPersonal : Form
    {
        IPersonalBL personalBL;
        
        public frmAEPersonal()
        {
            InitializeComponent();
            personalBL = new PersonalBL();
        }

        public Personal CurrentPers { get; set; }

        public void Editar()
        {
            personalBindingSource.DataSource = CurrentPers;
        }

        public void Nuevo()
        {
            Personal currentPersonal = personalBindingSource.Current as Personal;
            personalBindingSource.AddNew();
            currentPersonal = null;
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            if (dniTextBox.Text.Trim().Length == 8)
            {
                if (MessageBox.Show("¿Guardar Información?", "CONFIRMACIÓN", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    MessageBox.Show("¡Información Guardada Satisfactoriamente!", "AVISO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Personal CurrentPers = personalBindingSource.Current as Personal;
                    CurrentPers.cargo = cbbCargo.SelectedItem.ToString();
                    personalBL.RegistrarPersonal(CurrentPers);
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("El DNI debe tener 8 digitos!");
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
             
            this.Close();
        }

        private void cbbCargo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CurrentPers != null)
            {
                cbbCargo.SelectedItem = CurrentPers.cargo;
            }
            
        }

        private void frmAEPersonal_Load(object sender, EventArgs e)
        {
            cbbCargo.Items.Add("Gerente");
            cbbCargo.Items.Add("Vendedor");
            cbbCargo.Items.Add("Almacenero");
            cbbCargo.Items.Add("Despachador");

            cbbCargo.Focus();

            if (CurrentPers == null)
            {
                btnEditar.Enabled = false;
                cbbCargo.SelectedItem = "Gerente";
            }
            else
            {
                cbbCargo.SelectedItem = CurrentPers.cargo.Trim();
                btnEditar.Enabled = true;
            }            
        }

        #region Validaciones
        private void txtSoloTexto_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void txtSoloNumeros_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }            
            else
            {
                e.Handled = true;
            }
        }

        private void DNITextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(dniTextBox);
        }

        private void ap_PaternoTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(ap_pat_perTextBox);
        }

        private void ap_MaternoBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(ap_mat_perTextBox);
        }

        private void nombreTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(nombreTextBox);
        }

        private void telefonoTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(telefonoTextBox);
        }

        private void direccionTextBox_Validating(object sender, CancelEventArgs e)
        {
            ErrorProvider(direccionTextBox);
        }

        private void ErrorProvider(TextBox T)
        {
            if (T.Text.Trim().Length == 0)
            {
                erpAgregarEditarPersonal.SetError(T, "Debe Colocar un Dato");
                btnEditar.Enabled = false;
            }

            else
            {
                erpAgregarEditarPersonal.SetError(T, "");
                btnEditar.Enabled = true;
            }
        }
        
        #endregion

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

        }
    }
}
